import paho.mqtt.client as mqtt
from django.http import HttpResponse
from django.shortcuts import render
from app01 import models


def view(request,nid):
    if request.method == "GET":
        nid=request.GET.get(nid)
        return render(request, "mqtt.html")
    cmd = request.POST.get("cmd")
    print(cmd)
    cmd=str(nid)+'+'+cmd
    str_data = cmd
    client = mqtt.Client()
    # 参数有 Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp")
    client.connect("127.0.0.1", 1883, 60)  # 连接服务器,端口为1883,维持心跳为60秒
    # client.publish('test', cmd_list[0], 1)
    client.publish('test', str_data, 1)
    return render(request, "mqtt.html")

